import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		cartList: []
	},
	mutations: {
		addCart(state, payLoad) {
			// let index = state.cartList.indexOf(payLoad);

			// if (index === -1) {
			// 	state.cartList.push(payLoad);
			// } else {
			// 	state.cartList[index].count++;
			// }

			let product = state.cartList.find(function (item) {
				return item.iid === payLoad.iid;
			});

			if (!product) {
				state.cartList.push(payLoad);
			} else {
				product.count++;
			}
		}
	},
	getters: {
		cartLength(state) {
			return state.cartList.length;
		},
		cartList(state) {
			return state.cartList;
		}
	}
});

export default store;